package com.yimi.yinhepda.entity.response;

import com.yimi.yinhepda.utils.BigDecimalUtils;
import java.io.Serializable;
import java.util.List;

/**
 * @author 000432
 * @date 2017/4/22 0022
 */

public class DispatchScanResponse {

  private List<DispatchScanEntity> data;
  private boolean success;
  private String[] errorMessages;
  private int errorCode;

  public List<DispatchScanEntity> getData() {
    return data;
  }

  public void setData(List<DispatchScanEntity> data) {
    this.data = data;
  }

  public boolean isSuccess() {
    return success;
  }

  public void setSuccess(boolean success) {
    this.success = success;
  }

  public String[] getErrorMessages() {
    return errorMessages;
  }

  public void setErrorMessages(String[] errorMessages) {
    this.errorMessages = errorMessages;
  }

  public int getErrorCode() {
    return errorCode;
  }

  public void setErrorCode(int errorCode) {
    this.errorCode = errorCode;
  }

  /**
   * 总票
   */
  public int getTotalPiao() {
    if (data == null) {
      return 0;
    }
    return data.size();
  }

  /**
   * 总件
   */
  public int getTotalJian() {
    if (data == null || data.size() <= 0) {
      return 0;
    }
    int count = 0;
    for (DispatchScanEntity item : data) {
      if (item.getWaybillChildList() == null) {
        continue;
      }
      count += item.getWaybillChildList().size();
    }
    return data.size();
  }

  /**
   * 已经扫描总件数
   */
  public static int getScannedJian(List<DispatchScanEntity> data) {
    if (data == null || data.size() <= 0) {
      return 0;
    }
    int count = 0;
    for (DispatchScanEntity entity : data) {
      count += entity.getScanCount();
    }
    return count;
  }

  /**
   * 总已扫描票数
   */
  public static int getScannedPiao(List<DispatchScanEntity> data) {
    if (data == null || data.size() <= 0) {
      return 0;
    }
    int count = 0;
    List<DispatchScanResponse.DispatchWaybillChild> childList;
    for (DispatchScanEntity item : data) {
      childList = item.getWaybillChildList();
      if (childList == null) {
        continue;
      }
      for (DispatchScanResponse.DispatchWaybillChild childItem : childList) {
        if (childItem.getFlag() == 1) {
          count++;
          break;//加一次就好，退出当前for循环，继续
        }
      }
    }
    return count;
  }

  /**
   * 总重量
   */
  public static Double getTotalWeight(List<DispatchScanEntity> data) {
    if (data == null || data.size() <= 0) {
      return 0.0;
    }
    Double result = 0.0;
    for (DispatchScanEntity entity : data) {
      result += entity.getRealWeight();
    }
    return result;
  }

  /**
   * 总体积
   */
  public static Double getTotalVolume(List<DispatchScanEntity> data) {
    if (data == null || data.size() <= 0) {
      return 0.0;
    }
    Double result = 0.0;
    for (DispatchScanEntity entity : data) {
      result += entity.getVolume();
    }
    return result;
  }

  /**
   * 扫描件的总真实重量
   */
  public static Double getScannedWeight(List<DispatchScanEntity> data) {
    if (data == null || data.size() <= 0) {
      return 0.0;
    }
    Double result = 0.0;
    int count = 0;
    for (DispatchScanEntity entity : data) {
      count = entity.getScanCount();
      result += BigDecimalUtils.mul(BigDecimalUtils.div(entity.getRealWeight(), entity.getQuantity(), 4), count);
    }
    return result;
  }

  /**
   * 扫描件的总计费重量
   */
  public static Double getScannedChargeableWeight(List<DispatchScanEntity> data) {
    if (data == null || data.size() <= 0) {
      return 0.0;
    }
    Double result = 0.0;
    int count = 0;
    for (DispatchScanEntity entity : data) {
      count = entity.getScanCount();
      result += BigDecimalUtils.mul(BigDecimalUtils.div(entity.getChargeableWeight(), entity.getQuantity(), 4), count);
    }
    return result;
  }

  /**
   * 扫描件的总体积
   */
  public static Double getScannedVolume(List<DispatchScanEntity> data) {
    if (data == null || data.size() <= 0) {
      return 0.0;
    }
    Double result = 0.0;
    int count = 0;
    for (DispatchScanEntity entity : data) {
      count = entity.getScanCount();
      result += BigDecimalUtils.mul(BigDecimalUtils.div(entity.getVolume(), entity.getQuantity(), 4), count);
    }
    return result;
  }

  public static int getTotalPiaoNum(List<DispatchScanEntity> data) {
    if (data == null || data.size() <= 0) {
      return 0;
    }
    return data.size();
  }

  public static int getTotalJianNum(List<DispatchScanEntity> data) {
    if (data == null || data.size() <= 0) {
      return 0;
    }
    int result = 0;
    for (DispatchScanEntity entity : data) {
      result += entity.getQuantity();
    }
    return result;
  }

  /**
   * 运单信息
   */
  public static class DispatchScanEntity implements Comparable<DispatchScanEntity>, Serializable {

    private static final long serialVersionUID = 201L;
    /** 任务编号 */
    private String taskNo;
    /** 运单号 */
    private Long waybillNo;
    /** 到达网点 */
    private String destZoneCode;
    /** 到达网点名称 */
    private String destZoneName;
    /** 开单件数 */
    private Integer quantity;
    /** 库存件数 */
    private Integer storeQuantity;
    /** 配送扫描件数 */
    private Integer taskScanQuantity;
    /** 实重量 */
    private Double realWeight;
    private Double volume;
    /** 计费重量 */
    private Double chargeableWeight;
    /** 运单路由 */
    private String routeCode;
    /** 派件大区编号 */
    private String dispatchBigAreaCode;
    /** 派件大区名称 */
    private String dispatchBigAreaName;
    /** 派件区域编号 */
    private String dispatchAreaCode;
    /** 派件区域名称 */
    private String dispatchAreaName;
    /** 子单集合 */
    private List<DispatchWaybillChild> waybillChildList;

    public Integer getStoreQuantity() {
      return storeQuantity;
    }

    public void setStoreQuantity(Integer storeQuantity) {
      this.storeQuantity = storeQuantity;
    }

    public Double getChargeableWeight() {
      if (chargeableWeight == null) {
        chargeableWeight = 0.0;
      }
      return chargeableWeight;
    }

    public void setChargeableWeight(Double chargeableWeight) {
      this.chargeableWeight = chargeableWeight;
    }

    public void setDestZoneCode(String destZoneCode) {
      this.destZoneCode = destZoneCode;
    }

    public String getDestZoneCode() {
      return destZoneCode;
    }

    public String getDestZoneName() {
      return destZoneName;
    }

    public void setDestZoneName(String destZoneName) {
      this.destZoneName = destZoneName;
    }

    public String getDispatchBigAreaCode() {
      return dispatchBigAreaCode;
    }

    public void setDispatchBigAreaCode(String dispatchBigAreaCode) {
      this.dispatchBigAreaCode = dispatchBigAreaCode;
    }

    public String getDispatchBigAreaName() {
      return dispatchBigAreaName;
    }

    public void setDispatchBigAreaName(String dispatchBigAreaName) {
      this.dispatchBigAreaName = dispatchBigAreaName;
    }

    public String getDispatchAreaCode() {
      return dispatchAreaCode;
    }

    public void setDispatchAreaCode(String dispatchAreaCode) {
      this.dispatchAreaCode = dispatchAreaCode;
    }

    public String getTaskNo() {
      return taskNo;
    }

    public void setTaskNo(String taskNo) {
      this.taskNo = taskNo;
    }

    public Long getWaybillNo() {
      return waybillNo;
    }

    public void setWaybillNo(Long waybillNo) {
      this.waybillNo = waybillNo;
    }

    public Integer getQuantity() {
      return quantity;
    }

    public void setQuantity(Integer quantity) {
      this.quantity = quantity;
    }

    public Integer getTaskScanQuantity() {
      return taskScanQuantity;
    }

    public void setTaskScanQuantity(Integer taskScanQuantity) {
      this.taskScanQuantity = taskScanQuantity;
    }

    public Double getRealWeight() {
      if (realWeight == null) {
        realWeight = 0.0;
      }
      return realWeight;
    }

    public void setRealWeight(Double realWeight) {
      this.realWeight = realWeight;
    }

    public Double getVolume() {
      if (volume == null) {
        volume = 0.0;
      }
      return volume;
    }

    public void setVolume(Double volume) {
      this.volume = volume;
    }

    public String getRouteCode() {
      return routeCode;
    }

    public void setRouteCode(String routeCode) {
      this.routeCode = routeCode;
    }

    public String getDispatchAreaName() {
      return dispatchAreaName;
    }

    public void setDispatchAreaName(String dispatchAreaName) {
      this.dispatchAreaName = dispatchAreaName;
    }

    public List<DispatchWaybillChild> getWaybillChildList() {
      return waybillChildList;
    }

    public void setWaybillChildList(List<DispatchWaybillChild> waybillChildList) {
      this.waybillChildList = waybillChildList;
    }

    /**
     * 获得已扫数量
     */
    public int getScanCount() {
      if (waybillChildList == null || waybillChildList.size() == 0) {
        return 0;
      }
      int count = 0;
      for (DispatchScanResponse.DispatchWaybillChild item : waybillChildList) {
        if (item.getFlag() == 1) {
          count++;
        }
      }
      return count;
    }

    /**
     * 差异=扫描-开单
     */
    public int getDifference() {
      if (quantity == null) {
        quantity = 0;
      }
      return getScanCount() - quantity;
    }

    public int getType() {
      int dif = getDifference();
      if (getScanCount() == 0) {
        //白色
        return 1;
      } else if (dif < 0) {
        //红色
        return 0;
      } else if (dif > 0) {
        //黄色
        return 2;
      } else {
        //绿色
        return 3;
      }
    }

    @Override public int compareTo(DispatchScanEntity dispatchScanEntity) {
      return this.getType() - dispatchScanEntity.getType();
    }
  }

  /**
   * 运单子单集合
   */
  public static class DispatchWaybillChild implements Serializable {

    private static final long serialVersionUID = 202L;
    /** 主单号 */
    private Long waybillNO;
    /** 子单号 */
    private String childNo;
    /** 子单的状态 */
    private Integer flag;

    public Long getWaybillNO() {
      return waybillNO;
    }

    public void setWaybillNO(Long waybillNO) {
      this.waybillNO = waybillNO;
    }

    public String getChildNo() {
      return childNo;
    }

    public void setChildNo(String childNo) {
      this.childNo = childNo;
    }

    public Integer getFlag() {
      if (flag == null) {
        flag = 0;
      }
      return flag;
    }

    public void setFlag(Integer flag) {
      this.flag = flag;
    }
  }
}
